CROSS_COMPILE	?=	arm-linux-gnueabihf
TARGET			?=	ledc

CC				:= $(CROSS_COMPILE)-gcc
LD				:= $(CROSS_COMPILE)-ld
OBJCOPY			:= $(CROSS_COMPILE)-objcopy
OBJDUMP			:= $(CROSS_COMPILE)-objdump

INCLUDES		:= imx6u \
				   bsp/clk \
				   bsp/led \
				   bsp/delay

SRCDIRS			:= project \
				   bsp/clk \
				   bsp/led \
				   bsp/delay

INCLUDE			:= $(patsubst %,-I %,$(INCLUDES))
SFILES			:= $(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.S))
CFILES			:= $(foreach dir,$(SRCDIRS),$(wildcard $(dir)/*.c))

SFILEDIR		:= $(notdir $(SFILES))
CFILEDIR		:= $(notdir $(CFILES))

SOBJS			:= $(patsubst %, obj/%, $(SFILEDIR:.S=.o))
COBJS			:= $(patsubst %, obj/%, $(CFILEDIR:.c=.o))
OBJS			:= $(SOBJS)$(COBJS)

# 源文件搜索路径
VPATH			:= $(SRCDIRS)

$(TARGET).bin:$(OBJS)
	$(LD) -Timx6u.lds -o $(TARGET).elf $^
	$(OBJCOPY) -O binary -S $(TARGET).elf $@
	$(OBJDUMP) -D -m arm $(TARGET).elf > $(TARGET).dis

$(SOBJS):obj/%.o:%.S
	$(CC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ $<

$(COBJS):obj/%.o:%.c
	$(CC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ $<

.PHONY:clean print
clean:
	rm -rf $(OBJS) $(TARGET).elf $(TARGET).bin $(TARGET).dis

print:
	@echo INCLUDE=$(INCLUDE)
	@echo SFILEDIR=$(SFILEDIR)
	@echo CFILEDIR=$(CFILEDIR)
	@echo OBJS=$(OBJS)